package com.ruoyi.statistics.controller;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.statistics.domain.OrderChange;
import com.ruoyi.statistics.service.ISysStatisticsOrderService;
import com.ruoyi.system.service.ISysDeptService;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 订单统计曲线
 */
@RestController
@RequestMapping("/statistics/statisticsOrder")
public class SysStatisticsOrderController extends BaseController {
    @Autowired
    private ISysStatisticsOrderService sysStatisticsOrderService;
    @Autowired
    private ISysDeptService sysDeptService;

    @GetMapping("/getStatisticsOrder")
    public AjaxResult getStatisticsOrder(@RequestParam(value = "startTime") String startTimeStr, @RequestParam(value = "endTime") String endTimeStr) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date startTime = sdf.parse(startTimeStr);
        Date endTime = sdf.parse(endTimeStr);

        Map<String,List<OrderChange>> map = new HashMap<>();
        Long deptId = SecurityUtils.getDeptId();
        SysDept sysDept = sysDeptService.selectDeptById(deptId);

        if (sysDept.getType().equals("0")){
            List<OrderChange> list = sysStatisticsOrderService.getAllStatisticsOrder(startTime,endTime);
            map.put("list",list);
        }
        if (sysDept.getType().equals("1")){
            List<OrderChange> list = sysStatisticsOrderService.getStoreStatisticsOrder(startTime,endTime,deptId);
            map.put("list",list);
        }
        return AjaxResult.success(map);
    }

}
